﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_UserWagesController : BaseApiController
    {
        #region DI

        public PB_UserWagesController(IPB_UserWagesBusiness pB_UserWagesBus)
        {
            _pB_UserWagesBus = pB_UserWagesBus;
        }

        IPB_UserWagesBusiness _pB_UserWagesBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_UserWages>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_UserWagesBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_UserWages> GetTheData(IdInputDTO input)
        {
            return await _pB_UserWagesBus.GetTheDataAsync(input.id);
        }

        /// <summary>
        /// 验证工资是否有结算数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> CheckUserWageData(WagesDTO data)
        {
            return await _pB_UserWagesBus.CheckUserWageDataAsync(data);
        }

        /// <summary>
        /// 计算工资计件(按车间)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpUserWagesForNum(WagesDTO data)
        {
            await _pB_UserWagesBus.UpUserWagesForNumAsync(data);
        }

        /// <summary>
        /// 计算工资计重(按车间)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpUserWagesForWeight(WagesDTO data)
        {
            await _pB_UserWagesBus.UpUserWagesForWeightAsync(data);
        }

        /// <summary>
        /// 计算工资喷涂(按车间)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpUserWagesForPainting(WagesDTO data)
        {
            await _pB_UserWagesBus.UpUserWagesForPaintingAsync(data);
        }

        /// <summary>
        /// 计算工资劳务(按日结)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpUserWagesForLabourServicesByDay(WagesDTO data)
        {
            await _pB_UserWagesBus.UpUserWagesForLabourServicesByDayAsync(data);
        }

        /// <summary>
        /// 计算工资劳务(按月结)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpUserWagesForLabourServicesByMonth(WagesDTO data)
        {
            await _pB_UserWagesBus.UpUserWagesForLabourServicesByMonthAsync(data);
        }

        /// <summary>
        /// 计算工资单笔结算
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpUserWagesForLabourServicesBySingleSettlement(WagesDTO data)
        {
            await _pB_UserWagesBus.UpUserWagesForLabourServicesBySingleSettlementAsync(data);
        }

        /// <summary>
        /// 工资(全部)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<UserWagesListDTO> GetUserWages(WagesConditionDTO input)
        {
            return await _pB_UserWagesBus.GetUserWagesAsync(input);
        }

        /// <summary>
        /// 获取工序信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_ProcecureSub>> GetProcecureSub()
        {
            return await _pB_UserWagesBus.GetProcecureSubAsync();
        }

        /// <summary>
        /// 获取班别信息
        /// </summary>
        /// <param name="DeptId">车间Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_ShiftType>> GetShiftType(string DeptId)
        {
            return await _pB_UserWagesBus.GetShiftTypeAsync(DeptId);
        }

        /// <summary>
        /// 获取登录人所属工厂下的车间信息
        /// </summary>
        /// <param name="deptId">工厂Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Department>> GetWorkShop(string deptId)
        {
            return await _pB_UserWagesBus.GetWorkShopAsync(deptId);
        }
        /// <summary>
        /// 获取登录人所属工厂下的部门信息
        /// </summary>
        /// <param name="deptId">工厂Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Department>> GetGetBumen(string deptid)
        {
            return await _pB_UserWagesBus.GetBumenAsync( deptid);
        }
        /// <summary>
        /// 获取登录人所属工厂
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Department>> GetFactory()
        {
            return await _pB_UserWagesBus.GetFactoryAsync();
        }

        /// <summary>
        /// 工资信息导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<IActionResult> GetExportUserWagesList(WagesConditionDTO input)
        {
           var listdata =await _pB_UserWagesBus.GetUserWagesAsync(input);
            List<PB_UserWagesDTO> list = new List<PB_UserWagesDTO>();
            foreach(var item in listdata.UserWages){
                list.Add(item);
            }
            //创建EXCEL工作薄
            HSSFWorkbook workBook = new HSSFWorkbook();
            //创建sheet1文件表
            ISheet sheet = workBook.CreateSheet("工资");
            //创建表头
            IRow header = sheet.CreateRow(0);
            ICell cell = header.CreateCell(0);
            cell.SetCellValue("序号");
            cell = header.CreateCell(1);
            cell.SetCellValue("工序");
            cell = header.CreateCell(2);
            cell.SetCellValue("车间");
            cell = header.CreateCell(3);
            cell.SetCellValue("班别");
            cell = header.CreateCell(4);
            cell.SetCellValue("物料");
            cell = header.CreateCell(5);
            cell.SetCellValue("人员");
            cell = header.CreateCell(6);
            cell.SetCellValue("日期");
            cell = header.CreateCell(7);
            cell.SetCellValue("数量");
            cell = header.CreateCell(8);
            cell.SetCellValue("单价");
            cell = header.CreateCell(9);
            cell.SetCellValue("金额");

            for (int i = 0; i < list.Count; i++)
            {
                IRow rowData = sheet.CreateRow(i + 1);
                ICell cel = rowData.CreateCell(0);
                cel.SetCellValue(Convert.ToDouble(i+1));

                cel = rowData.CreateCell(1);
                cel.SetCellValue(list[i].SubName.ToString());

                cel = rowData.CreateCell(2);
                cel.SetCellValue(list[i].DeptName.ToString());
                cel = rowData.CreateCell(3);
                cel.SetCellValue(list[i].TeamName == null ? " " : list[i].TeamName.ToString());
                cel = rowData.CreateCell(4);
                cel.SetCellValue(list[i].MaterialName.ToString());

                cel = rowData.CreateCell(5);
                cel.SetCellValue(list[i].UserName==null?" ": list[i].UserName.ToString());

                cel = rowData.CreateCell(6);
                cel.SetCellValue(list[i].WorkDate.ToString());

                cel = rowData.CreateCell(7);
                cel.SetCellValue(Convert.ToDouble(list[i].Nums.ToString()));

                cel = rowData.CreateCell(8);
                cel.SetCellValue(Convert.ToDouble(list[i].Price.ToString()));

                cel = rowData.CreateCell(9);
                cel.SetCellValue(Convert.ToDouble(list[i].TotalPrice.ToString()));
            }
            //输出的文件名称
            string fileName = "工资" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            workBook.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);

        }


        /// <summary>
        /// 获取所有工艺下的工序
        /// </summary>
        /// <returns></returns>
       [HttpPost]
        public async Task<List<PB_ProcecureSub>> GetProcecureSubList() {
            return await _pB_UserWagesBus.GetProcecureSubList();
        }
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_UserWages data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_UserWagesBus.AddDataAsync(data);
            }
            else
            {
                await _pB_UserWagesBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_UserWagesBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}